// 封装本地缓存对象
const LocalCache = {
    // 写入缓存
    set(key, value) {
        try {
            const serializedValue = JSON.stringify(value);
            localStorage.setItem(key, serializedValue);
            console.log(`LocalCache: Successfully set ${key} to local storage.`);
        } catch (error) {
            console.error(`LocalCache: Error setting ${key} to local storage.`, error);
        }
    },

    // 读取缓存
    get(key) {
        try {
            const serializedValue = localStorage.getItem(key);
            if (serializedValue === null) {
                console.log(`LocalCache: ${key} not found in local storage.`);
                return null;
            }

            const value = JSON.parse(serializedValue);
            console.log(`LocalCache: Successfully retrieved ${key} from local storage.`);
            return value;
        } catch (error) {
            console.error(`LocalCache: Error retrieving ${key} from local storage.`, error);
            return null;
        }
    },

    // 清除指定缓存项
    remove(key) {
        try {
            localStorage.removeItem(key);
            console.log(`LocalCache: Successfully removed ${key} from local storage.`);
        } catch (error) {
            console.error(`LocalCache: Error removing ${key} from local storage.`, error);
        }
    },

    // 清空所有缓存
    clearAll() {
        try {
            localStorage.clear();
            console.log('LocalCache: Successfully cleared all items from local storage.');
        } catch (error) {
            console.error('LocalCache: Error clearing all items from local storage.', error);
        }
    }
};
export {
    LocalCache
}
// 示例
// LocalCache.set('user', { id: 1, name: 'John Doe' });
// const user = LocalCache.get('user');
// console.log('User:', user);

// 清空所有缓存（谨慎使用）
// LocalCache.clearAll();
